package com.sais.manage.boot.platform.service.impl;

import com.sais.common.utils.UUIDUtils;
import com.sais.manage.boot.platform.dto.DictItemDTO;
import com.sais.manage.boot.platform.dto.DictItemSaveDTO;
import com.sais.manage.boot.platform.entity.DictItem;
import com.sais.manage.boot.platform.mapper.DictItemMapper;
import com.sais.manage.boot.platform.service.IDictItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 宿舍管理系统-字典项 service 接口
 *
 * @author zhang
 * @date 2019/12/2
 **/
@Service
public class DictItemServiceImpl implements IDictItemService {


    @Autowired
    private DictItemMapper dictItemMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveDictItem(DictItemSaveDTO dictItemSaveDTO) {
        final String ukId = dictItemSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createDictItem(dictItemSaveDTO);
        } else {
            updateDictItem(dictItemSaveDTO);
        }
        return true;
    }

    @Override
    public boolean createDictItem(DictItemSaveDTO dictItemSaveDTO) {
        DictItem dictItem = dictItemSaveDTO.convert();
        dictItem.setUkId(UUIDUtils.getLower());
        dictItemMapper.createDictItem(dictItem);
        return true;
    }

    @Override
    public boolean updateDictItem(DictItemSaveDTO dictItemSaveDTO) {
        DictItem dictItem = dictItemSaveDTO.convert();
        dictItem.setGmtModified(LocalDateTime.now());
        dictItemMapper.updateById(dictItem);
        return true;
    }

    @Override
    public boolean deleteDictItem(String dictItemId) {
        dictItemMapper.deleteById(dictItemId);
        return true;
    }

    @Override
    public List<DictItemDTO> listDictItemByDictId(String dictId) {
        List<DictItemDTO> dictItems = dictItemMapper.listDictItemByDictId(dictId);
        return dictItems;
    }

    @Override
    public List<DictItemDTO> listFacultyDictItemByBuildingId(String buildingId) {
        List<DictItemDTO> dictItems = dictItemMapper.listFacultyDictItemByBuildingId(buildingId);
        return dictItems;
    }

    @Override
    public List<DictItemDTO> listDictItemByDictCode(String dictCode) {
        List<DictItemDTO> dictItems = dictItemMapper.listDictItemByDictCode(dictCode);
        return dictItems;
    }
}
